SPORE.$Import('core.request.base');
SPORE.$Import('core.json');

/*
---

name: Request.JSON

description: Extends the basic Request Class with additional methods for sending and receiving JSON data.

license: MIT-style license.

requires: [Request, JSON]

provides: Request.JSON

...
*/

SPORE.register('core.request.json',function($ns){

	Request.JSON = new Class({
	
		Extends: Request,
	
		options: {
			/*onError: function(text, error){},*/
			secure: true
		},
	
		initialize: function(options){
			this.parent(options);
			Object.append(this.headers, {
				'Accept': 'application/json',
				'X-Request': 'JSON'
			});
		},
	
		success: function(text){
			var json;
			try {
				json = this.response.json = JSON.decode(text, this.options.secure);
			} catch (error){
				this.fireEvent('error', [text, error]);
				return;
			}
			if (json == null) this.onFailure();
			else this.onSuccess(json, text);
		}
	
	});

});